﻿using PCTInterface.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility;

namespace PCTInterface
{
    public partial class FileTransmit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                try
                {
                    string fileName = Request["fileName"];
                    if (string.IsNullOrEmpty(fileName))
                    {
                        throw new NullReferenceException("关键字fileName未设置");

                    }
                    if (!System.IO.File.Exists(fileName))
                    {
                        throw new NullReferenceException("文件地址不存在," + fileName);
                    }
                    string compare_sign=  Request.Headers["REQUEST_TOKEN"];
                    if (string.IsNullOrEmpty(compare_sign))
                    {
                        throw new Exception("比对密钥不存在");
                    }

                    string current_sign = Tools.GenerateSign(fileName);
                    if (!compare_sign.Equals(current_sign)  )
                    {
                        throw new Exception("密钥校验错误");
                    }
                    Response.Headers["RETURN_CODE"] = "0";
                    Response.TransmitFile(fileName);
                }
                catch(Exception ex ) {
                    Logger.Instance.WriteEventLog("文件下载失败", ex.Message);
                    Response.Headers["RETURN_CODE"] = "99";
                    // Response.Write(JSONHelper.Instance.JavaScriptConvertMachion(new { RETURN_CODE = 99 }));
                }
            }
        }
    }
}